<?php

namespace App\Admin\Controllers;

use App\Land;
use App\City;
use App\LandUse;
use App\AreaLand;
use App\Broker;
use App\Http\Controllers\Controller;
use App\Standard;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Carbon\Carbon;

class LandController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('土地管理')
            ->description('土地管理')
            ->body($this->grid())->render();
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('土地详情')
            ->description('土地详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('土地编辑')
            ->description('土地编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建土地')
            ->description('新建土地')
            ->body($this->form());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Land);

        $grid->id('Id');
        $grid->code('厂房编号');
        $grid->is_out('来自')->display(function ($value) {
            if($value==0){
                return "来自本站";
            }else{
                return "来自58同城";
            }
        });
        $grid->p_city_id('区域');
        $grid->p_province_id('区域1');
        //$grid->slider('轮播图');
        $grid->title('标题');
        $grid->rent('租金');
        $grid->price('每亩价格');
        $grid->certificate('是否有土地证')->display(function ($value) {
            if ($value == 0) {
                return '否';
            } else if ($value == 1) {
                return '是';
            } 
        });
        $grid->area('面积');
        $grid->property('土地性质')->display(function ($value) {
            if ($value == 0) {
                return '出租';
            } else if ($value == 1) {
                return '出售';
            }
        });
        $grid->uses('规划用途')->display(function ($value) {
            return LandUse::find($value)->name ?? "";
        });
        $grid->address('地址');
        //$grid->standard_id('交付标准');
        $grid->property_cert('产权证')->display(function ($value) {
            if ($value == 0) {
                return '否';
            } else if ($value == 1) {
                return '是';
            }
        });
        $grid->rest_term('剩余土地年限');
        $grid->owner_release('是否业主发布')->display(function ($value) {
            if ($value == 0) {
                return '否';
            } else if ($value == 1) {
                return '是';
            }
        });
        $grid->click_count('点击量');
        $grid->call_count('电话量');
        $grid->telephone('经纪人');
        $grid->column('yulan', '预览')->display(function ($vlaue) {
            return "<a href='" . config('admin.app_outurl') . "/landDetail/{$this->id}' target='_blank'> 查看</a>";
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
  //筛选功能
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('certificate', '是否有土地证')->select([
                    ' ' => '全部',
                    0 => '无',
                    1 => '有',
                ]);
                $filter->equal('p_province_id', '城市')->select(City::getSelectOptions());
                if (\Request::get('p_province_id')) {
                    $filter->equal('p_city_id', '区域')->select(City::getSelectOptions(\Request::get('p_province_id')));
                }
                if (\Request::get('p_city_id')) {
                    $filter->equal('p_district_id', '下级区域')->select(City::getSelectOptions(\Request::get('p_city_id')));
                }
                $is_pro= [
                    0 => '出租',
                    1 => '出售',
                ];
                $filter->equal('property', '土地性质')->select($is_pro);
                $is_as= [
                    0 => '无',
                    1 => '有',
                ];
                $filter->equal('property_cert', '是否有产权证')->select($is_as);
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', '土地名称');
                $filter->like('code', '土地编号');
                $filter->equal('uses', '规划用途')->select(LandUse::getSelectOptions());
                $is_type = [
                    0 => '来自本站',
                    1 => '来自58同城',
                ];
                $filter->equal('is_out', '来源')->select($is_type);
            });
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Land::findOrFail($id));

        $show->id('Id');
        $show->code('厂房编号');
        $show->p_city_id('区域');
        $show->p_province_id('区域1');
        $show->slider('轮播图');
        $show->title('标题');
        $show->rent('租金');
        $show->price('每亩价格');
        $show->certificate('是否有土地证')->as(function ($value){
            if($value=1){
                return '是';
            }else{
                return '否';
            }
        });
        $show->area('面积');
        $show->property('土地性质')->as(function ($value){
            if($value=1){
                return '出售';
            }else{
                return '出租';
            }
        });
        $show->uses('规划用途')->as(function ($value){
            return LandUse::find($value)->name;
        });
        $show->address('地址');
        //$show->standard_id('交付标准');
        $show->property_cert('是否有产权证')->as(function ($value){
            if($value=1){
                return '是';
            }else{
                return '否';
            }
        });
        $show->rest_term('剩余土地年限');
        $show->is_out('来源')->as(function ($value) {
            if ($value == 0) {
                return "来自本站";
            } else {
                return "来自58同城";
            }
        });
        $show->owner_release('是否业主发布')->as(function ($value){
            if($value=1){
                return '是';
            }else{
                return '否';
            }
        });
        $show->click_count('点击量');
        $show->call_count('电话量');
        $show->telephone('经纪人');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Land);
        $form->tab('基础信息',function (Form $form){
            $is_type = [
                0 => '来自本站',
                1 => '来自58同城',
            ];
            $form->radio('is_out', '来源')->options($is_type);
            $is_pro= [
                0 => '出租',
                1 => '出售',
            ];
            $form->radio('property', '土地性质')->options($is_pro);
            $form->text('title', '标题');
            $form->select('p_province_id', '城市')->options(City::getSelectOptions())->load('p_city_id', '/admin/api/area')->rules('required', [
                'required' => '请选择城市'
            ]);
            $form->select('p_city_id', '区域')->options(function ($id) {
                $id = $id ?: old('p_city_id');
                if($id)
                return City::option($id);
            })->load('p_district_id', '/admin/api/area1')->rules('required', [
                'required' => '请选择区域'
            ]);
            $form->select('p_district_id', '下级区域')->options(function ($id) {
                $id = $id ?: old('p_district_id');
                if($id)
                return City::option($id);
            });
            $form->uploader('slider', '轮播图')->rules('required', [
                'required' => '请插入轮播图'
            ])->help('必填项');
            $is_own = [
                0 => '否',
                1 => '是',
            ];
            $form->radio('certificate', '是否有土地证')->options($is_own);

            $form->radio('uses', '规划用途')->options(LandUse::getSelectOptions());
            $form->tags('tag', '标签')->help('添加输入后点击空白处');
            $form->text('address', '地址');
            //$form->map('latitude', 'longitude', '地址与周边设施');
            $form->multipleSelect('standard_id', '交付标准')->options(Standard::getSelectOptions())->rules('required', [
                'required' => '请选择含有标准'
            ]);

            $form->currency('area', '面积')->symbol("㎡")->help('必填');
            /*$form->select('area_id', '面积范围')->options(AreaLand::getSelectOptions())->rules('required', [
                'required' => '请选择面积范围'
            ]);*/
            $form->radio('property_cert', '是否有产权证')->options($is_own);
            $form->currency('rest_term', '剩余土地年限')->symbol("年");
            $form->radio('owner_release', '是否业主发布')->options($is_own);
            //$form->number('click_count', '点击量');
            //$form->number('call_count', '电话量');
            $form->select('telephone', '经纪人名称')->options(Broker::getSelectOptions())->rules('required', [
                'required' => '请选择经纪人'
            ]);
            $form->hidden('code', '厂房编号')->default(Carbon::now()->timestamp);
            $states = [
                'on' => ['value' => 1, 'text' => '推荐', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '不推荐', 'color' => 'danger'],
            ];
            $form->switch('is_recommend', '是否推荐')->states($states);

            $form->lceditor('descs','描述');
        })->tab('出租',function (Form $form){

            $form->currency('rent', '租金')->symbol("元/月")->help('不填写则面议');
        })->tab('出售',function (Form $form){
            $form->currency('price', '每平价格')->symbol("元/㎡")->help('不填写则面议');
        });






        $transaction_type = [
            0 => '转让',
            1 => '直销',
        ];



        return $form;
    }
}
